package com.jd.mysql.mgr.controller.mgrdberror;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jd.mysql.mgr.dto.MgrDbErrorDTO;
import com.jd.mysql.mgr.pojo.MgrAccountGroup;
import com.jd.mysql.mgr.pojo.MgrDbError;
import com.jd.mysql.mgr.service.IMgrAccountService;
import com.jd.mysql.mgr.service.IMgrDbErrorService;
import com.jd.mysql.mgr.service.IMgrMachineService;
import com.jd.mysql.mgr.util.GlobalStaticVariables;
import com.jd.mysql.mgr.util.PageUtil;
import com.jd.mysql.mgr.util.ParamUtil;

public class MgrDbErrorController  extends MultiActionController {
	
	private IMgrAccountService mgrAccountService;
	private IMgrDbErrorService mgrDbErrorService;
	private IMgrMachineService mgrMachineService;
	
	public void setMgrAccountService(IMgrAccountService mgrAccountService) {
		this.mgrAccountService = mgrAccountService;
	}



	public void setMgrDbErrorService(IMgrDbErrorService mgrDbErrorService) {
		this.mgrDbErrorService = mgrDbErrorService;
	}



	public void setMgrMachineService(IMgrMachineService mgrMachineService) {
		this.mgrMachineService = mgrMachineService;
	}



	public ModelAndView toAddDbError(HttpServletRequest request,HttpServletResponse response) throws Exception {
		List<MgrAccountGroup> list = mgrAccountService.getMgrAccountGroupList();
		HttpSession session = request.getSession(true);
		String groupname = (String)session.getAttribute("groupname");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("groupname", groupname);
		map.put("groupList", list);
		return new ModelAndView("dberror/adddberror","mapgroup",map);
	}
	
	public ModelAndView dberrorlist(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String index = request.getParameter("index");
		Map<String,Object> map = new HashMap<String,Object>();
		Integer start = PageUtil.setPageParam(map,index, GlobalStaticVariables.limit);
		HttpSession session = request.getSession(true);
		Integer dbtype = (Integer)session.getAttribute("group");
		Long totalCount = mgrDbErrorService.getMgrDbErrorCount(dbtype);
		List<MgrDbError> dtoList = mgrDbErrorService.getMgrDbErrorList(dbtype, start, GlobalStaticVariables.limit);
		List<MgrDbErrorDTO> list = new ArrayList<MgrDbErrorDTO>();
		if(dtoList!=null){
			for(MgrDbError error : dtoList){
				list.add(error.toDTO());
			}
		}
		map.put("totalCount", totalCount);
		map.put("dtoList",list);
		return new ModelAndView("dberror/dberrorList","map",map);
	}
	
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Integer id = ParamUtil.getIntParam(request, "id", 0);
		MgrDbError dberror = mgrDbErrorService.findMgrDbErrorById(id);
		if(dberror!=null){
			mgrDbErrorService.deleteMgrDbError(dberror);
		}
		return this.dberrorlist(request, response);
	}
	
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Integer id = ParamUtil.getIntParam(request, "id", 0);
		Integer index  = ParamUtil.getIntParam(request, "index", 0);
		MgrDbError dberror = mgrDbErrorService.findMgrDbErrorById(id);
		MgrDbErrorDTO dto = dberror.toDTO();
		List<MgrAccountGroup> list = mgrAccountService.getMgrAccountGroupList();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("mgrdberror", dto);
		map.put("groupList", list);
		map.put("index", index);
		return new ModelAndView("dberror/adddberror","map",map);
	}
}
